<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="userName">
        <el-input
            v-model="queryParams.userName"
            placeholder="请输入姓名"
            clearable
            @keyup.enter="handleQuery"
            style="width: 200px"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="userNumber">
        <el-input
            v-model="queryParams.userNumber"
            placeholder="请输入身份证号"
            clearable
            @keyup.enter="handleQuery"
            style="width: 200px"
        />
      </el-form-item>
      <el-form-item label="性别" prop="userSex" >
        <el-select v-model="queryParams.userSex" placeholder="请选择性别" clearable
                   style="width: 200px">
          <el-option
              v-for="dict in sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="出生年月" prop="userBrithday">
        <el-date-picker clearable
                        v-model="queryParams.userBrithday"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择出生年月"
                        style="width: 200px">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="正式编制" prop="userEsta">
        <el-input
            v-model="queryParams.userEsta"
            placeholder="请输入正式编制"
            clearable
            @keyup.enter="handleQuery"
            style="width: 200px"
        />
      </el-form-item>
      <el-form-item label="部门名称" prop="userSection">
        <el-input
            v-model="queryParams.userSection"
            placeholder="请输入部门名称"
            clearable
            @keyup.enter="handleQuery"
            style="width: 200px"
        />
      </el-form-item>
      <el-form-item label="文化程度" prop="userEdu">
        <el-select v-model="queryParams.userEdu" placeholder="请选择文化程度" clearable style="width: 200px">
          <el-option
              v-for="dict in user_edu"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="主职务" prop="userJob" >
        <el-input
            v-model="queryParams.userJob"
            placeholder="请输入主职务"
            clearable
            @keyup.enter="handleQuery"
            style="width: 200px"
        />
      </el-form-item>
      <el-form-item label="籍贯" prop="userPlace">
        <el-input
            v-model="queryParams.userPlace"
            placeholder="请输入籍贯"
            clearable
            @keyup.enter="handleQuery"
            style="width: 200px"
        />
      </el-form-item>
      <el-form-item label="民族" prop="userNation">
        <el-input
            v-model="queryParams.userNation"
            placeholder="请输入民族"
            clearable
            @keyup.enter="handleQuery"
            style="width: 200px"
        />
      </el-form-item>
      <el-form-item label="入职日期" prop="userJobDate">
        <el-date-picker clearable
                        v-model="queryParams.userJobDate"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择入职日期"
                        style="width: 200px">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="入党日期" prop="userPartyDate">
        <el-date-picker clearable
                        v-model="queryParams.userPartyDate"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择入党日期"
                        style="width: 200px">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="政治面貌" prop="userPoliticsstatus">
        <el-select v-model="queryParams.userPoliticsstatus" placeholder="请选择政治面貌" clearable style="width: 200px">
          <el-option
              v-for="dict in user_politics_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="婚姻" prop="userMarriage">
        <el-select v-model="queryParams.userMarriage" placeholder="请选择婚姻" clearable style="width: 200px">
          <el-option
              v-for="dict in user_marriage"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['information:information:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['information:information:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['information:information:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['information:information:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="informationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="姓名" align="center" prop="userName" />
      <el-table-column label="身份证号" align="center" prop="userNumber" />
      <el-table-column label="性别" align="center" prop="userSex">
        <template #default="scope">
          <dict-tag :options="sys_user_sex" :value="scope.row.userSex"/>
        </template>
      </el-table-column>
      <el-table-column label="出生年月" align="center" prop="userBrithday" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.userBrithday, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="等级" align="center" prop="userGrade" />
      <el-table-column label="人员分类" align="center" prop="userClassify" />
      <el-table-column label="正式编制" align="center" prop="userEsta" />
      <el-table-column label="部门名称" align="center" prop="userSection" />
      <el-table-column label="文化程度" align="center" prop="userEdu">
        <template #default="scope">
          <dict-tag :options="user_edu" :value="scope.row.userEdu"/>
        </template>
      </el-table-column>
      <el-table-column label="主职务" align="center" prop="userJob" />
      <el-table-column label="籍贯" align="center" prop="userPlace" />
      <el-table-column label="民族" align="center" prop="userNation" />
      <el-table-column label="入职日期" align="center" prop="userJobDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.userJobDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="入党日期" align="center" prop="userPartyDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.userPartyDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="政治面貌" align="center" prop="userPoliticsstatus">
        <template #default="scope">
          <dict-tag :options="user_politics_status" :value="scope.row.userPoliticsstatus"/>
        </template>
      </el-table-column>
      <el-table-column label="婚姻" align="center" prop="userMarriage">
        <template #default="scope">
          <dict-tag :options="user_marriage" :value="scope.row.userMarriage"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['information:information:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['information:information:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改用户基础信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="informationRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="身份证号" prop="userNumber">
          <el-input v-model="form.userNumber" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="性别" prop="userSex">
          <el-select v-model="form.userSex" placeholder="请选择性别">
            <el-option
                v-for="dict in sys_user_sex"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="出生年月" prop="userBrithday">
          <el-date-picker clearable
                          v-model="form.userBrithday"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择出生年月">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="正式编制" prop="userEsta">
          <el-input v-model="form.userEsta" placeholder="请输入正式编制" />
        </el-form-item>
        <el-form-item label="部门名称" prop="userSection">
          <el-input v-model="form.userSection" placeholder="请输入部门名称" />
        </el-form-item>
        <el-form-item label="文化程度" prop="userEdu">
          <el-select v-model="form.userEdu" placeholder="请选择文化程度">
            <el-option
                v-for="dict in user_edu"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="主职务" prop="userJob">
          <el-input v-model="form.userJob" placeholder="请输入主职务" />
        </el-form-item>
        <el-form-item label="籍贯" prop="userPlace">
          <el-input v-model="form.userPlace" placeholder="请输入籍贯" />
        </el-form-item>
        <el-form-item label="民族" prop="userNation">
          <el-input v-model="form.userNation" placeholder="请输入民族" />
        </el-form-item>
        <el-form-item label="入职日期" prop="userJobDate">
          <el-date-picker clearable
                          v-model="form.userJobDate"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择入职日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="入党日期" prop="userPartyDate">
          <el-date-picker clearable
                          v-model="form.userPartyDate"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择入党日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="政治面貌" prop="userPoliticsstatus">
          <el-select v-model="form.userPoliticsstatus" placeholder="请选择政治面貌">
            <el-option
                v-for="dict in user_politics_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="婚姻" prop="userMarriage">
          <el-select v-model="form.userMarriage" placeholder="请选择婚姻">
            <el-option
                v-for="dict in user_marriage"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Information">
import { listInformation, getInformation, delInformation, addInformation, updateInformation } from "@/api/information/information";

const { proxy } = getCurrentInstance();
const { user_politics_status, user_edu, sys_user_sex, user_marriage } = proxy.useDict('user_politics_status', 'user_edu', 'sys_user_sex', 'user_marriage');

const informationList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userName: null,
    userNumber: null,
    userSex: null,
    userBrithday: null,
    userGrade: null,
    userClassify: null,
    userEsta: null,
    userSection: null,
    userEdu: null,
    userJob: null,
    userPlace: null,
    userNation: null,
    userJobDate: null,
    userPartyDate: null,
    userPoliticsstatus: null,
    userMarriage: null
  },
  rules: {
    userName: [
      { required: true, message: "姓名不能为空", trigger: "blur" }
    ],
    userNumber: [
      { required: true, message: "身份证号不能为空", trigger: "blur" }
    ],
    userNation: [
      { required: true, message: "民族不能为空", trigger: "blur" }
    ],
    userJobDate: [
      { required: true, message: "入职日期不能为空", trigger: "blur" }
    ],
    userPoliticsstatus: [
      { required: true, message: "政治面貌不能为空", trigger: "change" }
    ],
    userMarriage: [
      { required: true, message: "婚姻不能为空", trigger: "change" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询用户基础信息列表 */
function getList() {
  loading.value = true;
  listInformation(queryParams.value).then(response => {
    informationList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    userId: null,
    userName: null,
    userNumber: null,
    userSex: null,
    userBrithday: null,
    userGrade: null,
    userClassify: null,
    userEsta: null,
    userSection: null,
    userEdu: null,
    userJob: null,
    userPlace: null,
    userNation: null,
    userJobDate: null,
    userPartyDate: null,
    userPoliticsstatus: null,
    userMarriage: null
  };
  proxy.resetForm("informationRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.userId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加用户基础信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _userId = row.userId || ids.value
  getInformation(_userId).then(response => {
    console.log(response.data)
    console.log(response)
    form.value = response.data;
    open.value = true;
    title.value = "修改用户基础信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["informationRef"].validate(valid => {
    if (valid) {
      if (form.value.userId != null) {
        updateInformation(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addInformation(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _userIds = row.userId || ids.value;
  proxy.$modal.confirm('是否确认删除用户基础信息编号为"' + _userIds + '"的数据项？').then(function () {
    return delInformation(_userIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('information/information/export', {
    ...queryParams.value
  }, `information_${new Date().getTime()}.xlsx`)
}

getList();
</script>
